package com.tianqu.tms.www.finance.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.www.finance.actionform.PrepaymentSummaryForm;
import com.tianqu.tms.www.finance.viewhelper.PrepaymentSummaryViewHelper;

public class PrepaymentSummaryController extends TmsController {
	private Logger logger = Logger.getLogger(PrepaymentSummaryController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try {
			PrepaymentSummaryForm inputForm = (PrepaymentSummaryForm)uc.getInputForm();
			//
			String dateFrom = inputForm.getDateFrom().replace("-", "");
			String dateTo = inputForm.getDateTo().replace("-", "");
			IntegralRegister integralRegister = IntegralRegister.getInstance().findIntegralRegisterInfo(inputForm.getMemberId(), dateFrom,dateTo);
			//
			PrepaymentSummaryViewHelper view = new PrepaymentSummaryViewHelper();
			view.setAction(inputForm.getAction());
			view.setDateFrom(inputForm.getDateFrom());
			view.setDateTo(inputForm.getDateTo());
			view.setMemberId(inputForm.getMemberId());
			view.setCount(integralRegister.getCount());
			view.setAttnName(integralRegister.getAttnName());
			view.setShortName(integralRegister.getShortName());
			view.setTotalTransferPrice(integralRegister.getTotalTransferPrice());
			view.setSumTransferPrice(integralRegister.getSumTransferPrice());
			view.setInfo(integralRegister.getInfo());
			controller.commit();
			uc.setAttribute(TmsConstants.VIEWHELP, view);
		} catch (Exception e) {
			logger.error(e);
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
